#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:对外担保笔数
code:ExtGuarNum
tableName:
columnName:
groups:企业征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
ECRSummaryGuarantee:对外担保信息概要
Guaranteetype:担保类型
Amount：笔数
报告期中对外担保（包括：担保保证、担保抵押、担保质押）的笔数总和

'''

import sys
import pandas as pd
import datetime
import json

reload(sys)
sys.setdefaultencoding('utf-8')

#如果这个字段存在 但是里面的list长度为0 不包含任何dict 应该返回缺失值 不应该为0
def FormatData(tableName):
    try:
        ecr = json.loads(EC[0]["ecr"])[tableName]
        return ecr if isinstance(ecr, list) and len(ecr) > 0 else [{}]
    except:
        return [{}]

def ExGuarNum():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/', ' ']
    num = []
    try:
        ECRSummaryGuarantee = FormatData('ECRSummaryGuarantee')

        if ECRSummaryGuarantee == [{}]:
            return u'缺失值'
        else:
            if len(ECRSummaryGuarantee)>=1:
                for i in ECRSummaryGuarantee :
                    if i.has_key('Amount') and i['Amount'] not in null_type_list \
                            and isfloat(i['Amount']):
                        num.append(float(i['Amount']))
                    else:
                        num.append(None)
                if None in set(num) and len(set(num)) == 1:
                    return u'缺失值'
                else:
                    num_notnone = [i for i in num if i != None]
                    return sum(num_notnone)

                        # if i['Guaranteetype'] == '保证汇总':
                        #     num.append(float(i['Amount']))
                        # if i['Guaranteetype'] == '抵押汇总':
                        #     num.append(float(i['Amount']))
                        # if i['Guaranteetype'] == '质押汇总':
                        #     num.append(float(i['Amount']))


            else:
                return u'缺失值'
    except:
        return u'缺失值'

def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            return True
        else:
            return False
    except:
        return False

result = ExGuarNum()